package org.adl.registry.website;

import java.io.IOException;
import javax.servlet.ServletContext;
import org.adl.registry.website.rim.RimProxy;

/**
 *
 * @author marksj
 */
public abstract class StatusRequestHandler extends SubmissionRequestHandler
{
    public static enum StatusCheckTypes
    {
        Validation,
        Transaction,
        Unknown
    }
    
    private String mCommand;
    private String mUserId;
    private String mPassword;
    private String mTargetIdentifier;
    private String mResponseType;
    private StatusRequestHandler.StatusCheckTypes mRequestType;
    
    /** Creates a new instance of StatusRequestHandler */
    public StatusRequestHandler()
    {
        this.mRequestType = StatusRequestHandler.StatusCheckTypes.Unknown;
    }
    
    /**
     *
     * @return
     * @param iRimUrl
     * @param iClientAccessKey
     * @param iRegistry
     * @param iQuery
     * @throws java.io.IOException
     */
    public String sendStatusRequest()
    throws IOException
    {
        String rimResponse;
        
        ServletContext context = super.getServletContext();
        
        RimProxy p = new RimProxy(super.getRimUrl());
        p.openConnection();
        
        p.writeParam("command", this.mCommand);
        
        p.writeParam("cak", super.getClientAccessKey());
        p.writeParam("registry", super.getRegistry());
        
        if ((this.mResponseType != null) && (this.mResponseType.length() > 0))
        {
            p.writeParam("rtype", this.mResponseType);
        }
        
        if (this.mRequestType == StatusRequestHandler.StatusCheckTypes.Transaction)
        {
            p.writeParam("transactionID", this.mTargetIdentifier);
        }
        if (this.mRequestType == StatusRequestHandler.StatusCheckTypes.Transaction)
        {
            p.writeParam("validationID", this.mTargetIdentifier);
        }
        
        p.writeParam("password", this.mPassword);
        p.writeParam("userid", this.mUserId);
        
        rimResponse = p.writeResponseTo();
        
        p.close();
        
        return rimResponse;
    }

    public String getCommand()
    {
        return this.mCommand;
    }

    public void setCommand(String iCommand)
    throws InvalidRequestException
    {
        if ((iCommand == null) || (iCommand.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iCommand = iCommand.trim();
        
        this.mCommand = iCommand;
    }

    public String getUserId()
    {
        return this.mUserId;
    }

    public void setUserId(String iUserId)
    throws InvalidRequestException
    {
        if ((iUserId == null) || (iUserId.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iUserId = iUserId.trim();
        
        this.mUserId = iUserId;
    }

    public String getPassword()
    {
        return this.mPassword;
    }

    public void setPassword(String iPassword)
    throws InvalidRequestException
    {
        if ((iPassword == null) || (iPassword.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iPassword = iPassword.trim();
        
        this.mPassword = iPassword;
    }

    public String getTargetIdentifier()
    {
        return this.mTargetIdentifier;
    }

    public void setTargetIdentifier(String iTargetIdentifier)
    throws InvalidRequestException
    {
        if ((iTargetIdentifier == null) || (iTargetIdentifier.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iTargetIdentifier = iTargetIdentifier.trim();
        
        this.mTargetIdentifier = iTargetIdentifier;
    }

    public String getResponseType()
    {
        return this.mResponseType;
    }

    public void setResponseType(String iResponseType)
    throws InvalidRequestException
    {
        if ((iResponseType == null) || (iResponseType.trim().length() == 0))
        {
            throw new InvalidRequestException(".");
        }
        
        iResponseType = iResponseType.trim();
        
        this.mResponseType = iResponseType;
    }
    

    public StatusRequestHandler.StatusCheckTypes getRequestType()
    {
        return mRequestType;
    }

    public void setRequestType(StatusRequestHandler.StatusCheckTypes iRequestType)
    throws InvalidRequestException
    {
        if (iRequestType == StatusRequestHandler.StatusCheckTypes.Unknown)
        {
            throw new InvalidRequestException(".");
        }

        this.mRequestType = iRequestType;
    }
}
